package com.prs.crm.service.stock.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.dao.stock.ProductDao;
import com.prs.crm.domain.stock.ClassAttribute;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductAttrValue;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.stock.ProductClassService;
import com.prs.crm.service.stock.ProductService;

public class ProductServiceImpl extends BaseServiceImpl<Product,ProductDao> implements ProductService {
	
	@Autowired
	private ProductClassService productClassService;
	
	public void setProductClassService(ProductClassService productClassService) {
		this.productClassService = productClassService;
	}

	public ProductClassService getProductClassService() {
		return productClassService;
	}

	public 	void saveProduct(Product product,Integer[] attrs,String[] attrvals)
	{
		if (attrs!=null)
		{
			int i=0;
			for (Integer attrid : attrs)
			{
				ClassAttribute attr = this.getProductClassService().getAttribute(attrid);
				ProductAttrValue val = product.getAttrValue(attr);
				if (val==null)
				{
					product.setAttrValue(attr,attrvals[i]);
				}
				else
				{
					val.setValue(attrvals[i]);
				}
				i++;
			}
		}
		super.save(product);
	}

	@Override
	public List<Product> getBatchNum() {		
		return this.getDao().getBatchNum();
	}

	@Override
	public Product getProductBycode(String productcode) {
		// TODO Auto-generated method stub
		return this.getDao().getProductBycode(productcode);
	}

	@Override
	public void saveOrProduct(Product product) {
		// TODO Auto-generated method stub
		this.getDao().saveOrProduct(product);
	}
}
